package com.sc.controller.property;


import cn.hutool.core.bean.BeanUtil;
import com.sc.domain.Announce;
import com.sc.dto.AnnounceDTO;
import com.sc.dto.AnnouncePageDTO;
import com.sc.result.PageResult;
import com.sc.result.Result;
import com.sc.service.AnnounceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Api(tags = "物流服务-公告管理")
@RestController
@RequestMapping("/property/announce")
@CrossOrigin
public class AnnounceController {

    private final AnnounceService announceService;

    @PostMapping("/add")
    @ApiOperation("添加公告")
    public Result addAnnounce(@RequestBody AnnounceDTO announceDTO) {
        log.info("添加公告：{}", announceDTO);
        announceService.saveAnnounce(announceDTO);
        return Result.success();
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除单个公告")
    public Result removeAnnounce(@PathVariable Integer id) {
        log.info("删除单个公告：{}", id);
        announceService.removeById(id);
        return Result.success();
    }


    @DeleteMapping("/remove/batch")
    @ApiOperation("删除多个公告")
    public Result removeByIds(@RequestParam List<Integer> ids) {
        log.info("删除多个公告：{}", ids);
        announceService.removeByIds(ids);
        return Result.success();
    }

    @PutMapping("/modify")
    @ApiOperation("修改公告")
    public Result modifyAnnounce(@RequestBody AnnounceDTO announceDTO) {
        log.info("修改公告：{}", announceDTO);
        announceService.updateAnnounce(announceDTO);
        return Result.success();
    }


    @GetMapping("/pageQuery")
    @ApiOperation("分页查询活动")
    public Result<PageResult<Announce>> pageQuery(AnnouncePageDTO announcePageDTO) {
        log.info("公告分页查询：{}", announcePageDTO);
        PageResult<Announce> result = announceService.pageQuery(announcePageDTO);
        return Result.success(result);
    }

    @GetMapping("/query/{id}")
    @ApiOperation("根据id查询活动")
    public Result<AnnounceDTO> pageQuery(@PathVariable Integer id) {
        log.info("根据id查询活动：{}", id);
        Announce announce = announceService.getById(id);
        return Result.success(BeanUtil.copyProperties(announce, AnnounceDTO.class));
    }
}
